package com.dcms.power.controller;

import com.dcms.power.annotation.ArchivesLog;
import com.dcms.admin.controller.BaseController;
import com.dcms.power.bean.Role;
import com.dcms.power.bean.RoleMenu;
import com.dcms.power.dao.RoleMenuMapper;
import com.dcms.power.service.IRoleService;
import com.dcms.util.StringUtil;
import com.dcms.util.layui.LayTable;
import com.dcms.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * \*
 * \* @name: RoleController
 * \* @author: 武佳伟丶
 * \* @date: 2018/4/8 0008
 * \* @time: 19:23
 * \* @description: To change this template use File | Settings | File Templates.
 * \
 */
@Controller
@RequestMapping("power/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @RequestMapping("query")
    @ResponseBody
    public LayTable<Role> query(
            String param,
            Role record,
            Integer limit,
            Integer page
    ){
        page = page == null ? 1 : page;
        limit = limit == null ? 12 : limit;

        PageHelper.startPage(page, limit);
        List<Role> list = roleService.selectAll(param, record);
        PageInfo<Role> info = new PageInfo<Role>(list);
        LayTable<Role> bean = new LayTable<Role>(info.getList(), (int)info.getTotal());
        return bean;
    }

    /**
     * @author: 武佳伟丶
     * @description: 删除，支持批量
     * @date: 14:39 2018/2/12 0012
     * @param: [id]
     * @return: com.dcms.util.ResultBean
     */
    @ArchivesLog(message = "删除角色")
    @RequestMapping("delete")
    @ResponseBody
    public ResultBean deleteBatch(@RequestParam(value = "id[]", required = true)Integer[] id){
        int rows = roleService.deleteBatch(id);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 更新
     * @date: 18:55 2018/2/13 0013
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    @ArchivesLog(message = "更新角色信息")
    public ResultBean update(Role record){
        record.setUpdateTime(new Date());
        int rows = roleService.updateByPrimaryKeySelective(record);
        if (rows > 0){
            return new ResultBean();
        }
        return new ResultBean(10021);
    }

    /**
     * @author: 武佳伟丶
     * @description: 跳转到数据编辑页，用于新增数据
     * @date: 14:42 2018/2/18 0018
     * @param: [roleId, request]
     * @return: java.lang.String
     */
    @RequestMapping("toinsert")
    public String toInsert(){
        return "power/role/edit";
    }

    /**
     * @author: 武佳伟丶
     * @description: 录入数据
     * @date: 14:52 2018/2/18 0018
     * @param: [record]
     * @return: com.dcms.util.ResultBean
     */
    @RequestMapping("insert")
    @ResponseBody
    @ArchivesLog(message = "录入新的角色")
    @Transactional(rollbackFor = Exception.class)
    public ResultBean insert(Role record, String menuIdsStr){
        Date now = new Date();
        int userId = getCurrentUser().getUserId();
        record.setCtime(now);
        record.setUpdateTime(now);
        record.setCreator(userId);
        int rows = roleService.insertSelective(record);
        if (rows > 0){
            //录入角色菜单关联表
            if (StringUtil.isNotEmpty(menuIdsStr)){
                String[] ids = menuIdsStr.split(",");
                for (int i = 0; i < ids.length; i ++){
                    RoleMenu roleMenu = new RoleMenu();
                    roleMenu.setCreator(userId);
                    roleMenu.setCtime(now);
                    roleMenu.setUpdateTime(now);
                    roleMenu.setMenuId(Integer.valueOf(ids[i]));
                    roleMenu.setRoleId(record.getRoleId());
                    roleMenuMapper.insertSelective(roleMenu);
                }
            }

            return  new ResultBean();
        }
        return new ResultBean();
    }
}